package com.tanhua.dubbo.api.mongo;

import com.tanhua.domain.mongo.UserLocation;
import com.tanhua.vo.UserLocationVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Date;
import java.util.List;

@Service
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Long userId, Double longitude, Double latitude, String addrStr) {

//        判断当前用户是否有userLocation对应的值

        Query query = new Query(Criteria.where("userId").is(userId));
        if(mongoTemplate.exists(query,UserLocation.class)){
//            这个用户存在UserLocation对象  那么就是要更新操作
//            update user_location set Location=? and  Address=?? where userId=??
            Update update = new Update();
            update.set("address",addrStr);
            update.set("location",new GeoJsonPoint(longitude,latitude));
            update.set("lastUpdated",new Date().getTime());
            mongoTemplate.updateFirst(query,update,UserLocation.class);
        }else{
            UserLocation userLocation = new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setId(new ObjectId());
//            MongoDB专门用来存储地理信息的对象GeoJsonPoint
            GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude,latitude);
            userLocation.setLocation(geoJsonPoint); //经纬度
            userLocation.setAddress(addrStr);

            userLocation.setCreated(new Date().getTime());
            userLocation.setUpdated(new Date().getTime());
            userLocation.setLastUpdated(new Date().getTime());

            mongoTemplate.save(userLocation);
        }
    }

    @Override
    public List<UserLocationVo> search(Long userId,Integer distance) {
//        以当前登录人为中心 以distance为半径查询
        Query query = new Query(Criteria.where("userId").is(userId));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        GeoJsonPoint geoJsonPoint = userLocation.getLocation();
//        以geoJsonPoint为中心画一个圆圈
        Circle circle = new Circle(geoJsonPoint,new Distance(distance/1000, Metrics.KILOMETERS));
        Query circleQurey = new Query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(circleQurey, UserLocation.class);

        List<UserLocationVo> userLocationVos = UserLocationVo.formatToList(userLocationList);

        return userLocationVos;
    }
}
